package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaItemDictDto;
import com.ysd.lis.dto.sta.StaItemDictParam;
import com.ysd.lis.entity.sta.StaItemDictCritical;
import com.ysd.lis.mapper.sta.StaItemDictCriticalMapper;
import com.ysd.lis.service.sta.StaItemDictCriticalService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaItemDictCriticalServiceImpl extends ServiceImpl<StaItemDictCriticalMapper, StaItemDictCritical> implements StaItemDictCriticalService {

    @Autowired
    StaItemDictCriticalMapper staItemDictCriticalMapper;
    @Override
    public Result findStaItemDictCritical(StaItemDictParam param) {
        if (ToolsUtils.isEmpty(param.getItemNo())) {
            return Result.fail("报告项目ID不可为空");
        }

        MPJLambdaWrapper<StaItemDictCritical> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(StaItemDictCritical.class)
                .eq(StaItemDictCritical::getItemNo, param.getItemNo())
                .eq(StaItemDictCritical::getDelFlag, 0);
        List<StaItemDictCritical> list = staItemDictCriticalMapper.selectList(lambdaQueryWrapper);
        return Result.succ(list);
    }

    @Override
    public Result deleteStaItemCritical(StaItemDictDto dto) {
        if (ToolsUtils.isEmpty(dto.getId())) {
            return Result.fail("入参不可为空！");
        }
        //是否需要判断有没有相关业务正在使用这个规则，先走流程。此处需要确认
        int delete = staItemDictCriticalMapper.deleteById(dto.getId());
        if (delete > 0) {
            return Result.succ(1, "删除成功！", null);
        } else {
            return Result.fail("删除失败！");
        }
    }

    @Override
    public Result saveOrUpdateStaCritical(List<StaItemDictCritical> staItemDictCriticals) {
        boolean b = this.saveOrUpdateBatch(staItemDictCriticals);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }
}
